<template>
  <div class="app-container">
    <el-card :bordered="false">
      <el-form  ref="formValidate" :rules="ruleValidate" :model="formValidate" label-width="130px" >
        <!-- <el-row :gutter="24"> -->
           <el-row :gutter="24">
          <!-- 商品信息-->
          <el-col v-bind="grid2">
            <el-form-item label="商户名称：" prop="proName">
              <el-input v-model="formValidate.proName" placeholder="请输入商品名称" :disabled="identNumber == 3" />
            </el-form-item>
          </el-col>
         
            <el-col v-bind="grid2">
            
              <el-form-item label="商品分类" prop="cateId">
                  <el-select v-model="formValidate.cateId" clearable placeholder="请选择商品类型" class="filter-item" style="width: 100%" :disabled="identNumber == 3">
                   <el-option v-for="item in queryTypeOptions"  :key="item.value" :label="item.label" :value="item.value" />
                  </el-select> 
            </el-form-item>
            </el-col>
              <el-col v-bind="grid2">
              <el-form-item label="所属商铺：" prop="merId">
                 <el-select v-model="formValidate.merId" clearable placeholder="请选择所属商铺" class="filter-item" style="width: 100%" :disabled="identNumber == 3">
                   <el-option v-for="item in shopsTypeOptions" :key="item.merId" :label="item.merName" :value="item.merId" />
                  </el-select>
            </el-form-item>
           </el-col>
            <el-col v-bind="grid2"  :lg="6">
            <el-form-item label="让利比例：" prop="yieldAmount">
              <el-input v-model.number="formValidate.yieldAmount" placeholder="请输入让利比例" style="width:85%"  :disabled="identNumber == 3"/> %
            </el-form-item>
           </el-col>
              <el-col v-bind="grid2" :lg="6">
            <el-form-item label="让利金额：" prop="yieldProfits">
              <el-input v-model.number="formValidate.yieldProfits" placeholder="请输入让利金额"   :disabled="identNumber == 3"/>
            </el-form-item>
           </el-col>
              <el-col v-bind="grid2">
            <el-form-item label="库存：" prop="stock">
              <el-input v-model.number="formValidate.stock" placeholder="请输入库存"  :disabled="identNumber == 3"/>
            </el-form-item>
           </el-col>
           <el-col v-bind="grid2">
            <el-form-item label="商品简介：" prop="proInfo">
              <el-input v-model="formValidate.proInfo" placeholder="请输入商品简介"  type="textarea" :row="3" :disabled="identNumber == 3"/>
            </el-form-item>
           </el-col>
           </el-row>
           <div  style="text-align:left" >
            <el-form-item label="商户封面图：" prop="image">
              <single-pic v-model="formValidate.image"  type="image" :num="1" :width="150" :height="150" :disabled="identNumber == 3"/>
            </el-form-item>
          </div>
          <div>
              <el-form-item label="商品轮播图：" prop="slider_Image" style="text-align:left">
              <MaterialList v-model="formValidate.slider_Image"  type="image" :num="4" :width="150" :height="150" :disabled="identNumber == 3"/>
            </el-form-item>
          </div>
          <div >
            <el-form-item label="商品详情：" prop="description">
              <ueditor-wrap v-model="formValidate.description" :config="myConfig"  @beforeInit="addCustomDialog" :disabled="identNumber == 3"></ueditor-wrap>
            </el-form-item>
          </div>
          <div>
             <el-form-item label="排序">
        <el-input-number v-model.number="formValidate.sort" style="width: 150px;" :disabled="identNumber == 3" />
      </el-form-item>
          </div>
            <div class="radioAlign">
             <!-- <el-form-item label="审核结果："  prop="" v-if="identNumber == 2">
              <el-radio-group v-model="formValidate.result"  @change="changeSpec">
                <el-radio :label="0" class="radio">通过</el-radio>
                <el-radio :label="1">不通过</el-radio>
              </el-radio-group>
            </el-form-item> -->
         <el-form-item>
           <el-button v-if="identNumber==3" type="primary" class="submission" @click="handleBack">返回</el-button>
          <el-button v-if="identNumber!=3" type="primary" class="submission" @click="handleSubmit('formValidate')">提交</el-button>
          <el-button v-if="identNumber!=3" type="" class="submission" @click="handleCancel('formValidate')">取消</el-button>
       
        </el-form-item>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import {
  add,
  getListByUser,
  getInfo,
  update,
  getStoreCategoryList,
} from "@/api/productManage";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import editor from "../../components/Editor";
import singlePic from "@/components/singlematerial";
import crudYxSystemStore from "@/api/yxSystemStore";
import UeditorWrap from "vue-ueditor-wrap";
import MaterialList from "@/components/material";
const defaultCrud = CRUD({
  title: "门店",
  url: "api/yxSystemStore",
  sort: "id,desc",
  crudMethod: { ...crudYxSystemStore },
});
export default {
  components: {
    editor,
    crudYxSystemStore,
    singlePic,
    UeditorWrap,
    MaterialList,
  },
  data() {
    return {
      myConfig: {
        autoHeightEnabled: false, // 编辑器不自动被内容撑高
        initialFrameHeight: 500, // 初始容器高度
        initialFrameWidth: "100%", // 初始容器宽度
        UEDITOR_HOME_URL: "/UEditor/",
        serverUrl: "",
      },
      spinShow: false,
      queryTypeOptions: [],
      shopsTypeOptions: [],
      identNumber: "",
      grid2: {
        xl: 10,
        lg: 12,
        md: 12,
        sm: 24,
        xs: 24,
      },
      grid3: {
        xl: 18,
        lg: 18,
        md: 20,
        sm: 24,
        xs: 24,
      },

      formValidate: {
        cateId: "",
        proName: "",
        merId: "",
        yieldAmount: null,
        yieldProfits: null,
        proInfo: "",
        stock: null,
        image: "",
        slider_Image: [],
        sliderImage: "",
        description: "",
        sort: 0,
        keyword: "",
      },
      images: [],
      grid: {
        xl: 8,
        lg: 8,
        md: 12,
        sm: 24,
        xs: 24,
      },
      ruleValidate: {
        proName: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
        ],
        store_info: [
          { required: true, message: "请输入商品简介", trigger: "blur" },
        ],
      },
    };
  },
  watch: {},
  mounted() {
    this.getInfo();
    this.getListByUser();
    this.getStoreCategoryList();
  },
  methods: {
    // 详情
    getInfo() {
      let that = this;
      let nodeId = that.$route.params.id || 0;
      that.identNumber = that.$route.query.identNumber;
      getInfo(nodeId).then(async res => {
        let data = res;
        if (data) {
          that.formValidate = data;
          that.formValidate.cateId = Number(data.cateId);
          that.formValidate.slider_Image = data.sliderImage.split(",");
          //编辑
        }
      });
    },
    //获取所属商铺下拉框
    getListByUser() {
      getListByUser().then(res => {
        this.shopsTypeOptions = res.content;
      });
    },
    //商户分类列表
    getStoreCategoryList() {
      getStoreCategoryList().then(res => {
        this.queryTypeOptions = res;
      });
    },
    addCustomDialog() {
      window.UE.registerUI(
        "yshop",
        function(editor, uiName) {
          let dialog = new window.UE.ui.Dialog({
            iframeUrl: "/yshop/materia/index",
            editor: editor,
            name: uiName,
            title: "上传图片",
            cssRules: "width:1200px;height:500px;padding:20px;",
          });
          this.dialog = dialog;

          var btn = new window.UE.ui.Button({
            name: "dialog-button",
            title: "上传图片",
            cssRules: `background-image: url(../../../assets/images/icons.png);background-position: -726px -77px;`,
            onclick: function() {
              dialog.render();
              dialog.open();
            },
          });

          return btn;
        },
        37
      );
    },
    // 改变审核结果
    changeSpec() {},
    // 提交
    handleSubmit(name) {
      this.$refs[name].validate(valid => {
        if (valid) {
          //添加
          if (this.identNumber == 1) {
            this.formValidate.sliderImage = this.formValidate.slider_Image.toString();
            this.formValidate.keyword = this.formValidate.slider_Image.toString();
            add(this.formValidate)
              .then(async res => {
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$router.push({
                    path: "/storeCommodityManage/productList",
                  });
                }, 500);
              })
              .catch(res => {
                // this.$message({
                //   message:res.message,
                //   type: 'error'
                // });
              });
          } else if (this.identNumber == 2) {
            //编辑
            update(this.formValidate)
              .then(async res => {
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
                setTimeout(() => {
                  this.$router.push({
                    path: "/storeCommodityManage/productList",
                  });
                }, 500);
              })
              .catch(res => {});
          }
        }
      });
    },
    handleBack() {
      this.$router.push({
        path: "/storeCommodityManage/productList",
      });
    },
    // 取消-清空表单
    handleCancel(name) {
      this.formValidate.proName = "";
      this.formValidate.cateId = "";
      this.formValidate.merId = "";
      this.formValidate.yieldAmount = "";
      this.formValidate.yieldProfits = "";
      this.formValidate.stock = "";

      this.formValidate.proInfo = "";
      this.formValidate.image = "";
      this.formValidate.slider_Image = [];
      this.formValidate.description = "";
      this.formValidate.sort = 0;
      this.formValidate.stock = "";
    },
    // 表单验证
    validate(prop, status, error) {
      if (status === false) {
        this.$message.warning(error);
      }
    },
  },
};
</script>

<style scoped lang="stylus">
.el-form-item--small.el-form-item {
  margin-bottom: 20px;
}

.submission {
  margin-left: 10px;
}

.color-list .tip {
  color: #c9c9c9;
}

.color-list .color-item {
  height: 30px;
  line-height: 30px;
  padding: 0 10px;
  color: #fff;
  margin-right: 10px;
}

.color-list .color-item.blue {
  background-color: #1E9FFF;
}

.color-list .color-item.yellow {
  background-color: rgb(254, 185, 0);
}

.color-list .color-item.green {
  background-color: #009688;
}

.columnsBox {
  margin-right: 10px;
}

.priceBox {
  width: 100%;
}

.rulesBox {
  display: flex;
  flex-wrap: wrap;
}

.curs {
  cursor: pointer;
}
</style>
